define([
    'config',
    'angularAMD',
    'js/init',
    'css!css/default'
], function(config, angularAMD) {
    var app = angular.module('app', ['app.main']);

    app.config(function($routeProvider, $httpProvider, cfpLoadingBarProvider) {
        $routeProvider.otherwise({
            redirectTo: config.defaultPath
        });

        // loading bar's settings
        cfpLoadingBarProvider.includeBar = true;
        cfpLoadingBarProvider.includeSpinner = false;

        // http interceptor
        $httpProvider.interceptors.push(function($injector) {
            return {
                responseError: function(rejection) {
                    // error
                    if (rejection.status == -1) {
                        $injector.get('xDialog').alert('错误信息', '服务器已断开连接或连接超时！');
                    } else if (rejection.status >= 400) {
                        if (!/^\/mock\/test\//.test(rejection.config.url)) {
                            $injector.get('xDialog').alert('错误信息', rejection.data);
                        }
                    }
                    return rejection;
                }
            };
        });
    });

    app.run(function($rootScope, $route, $timeout, $location) {
        // override location path
        var original = $location.path;
        $location.path = function(path, reload) {
            if (reload === false) {
                var lastRoute = $route.current;
                var un = $rootScope.$on('$locationChangeSuccess', function() {
                    $route.current = lastRoute;
                    un();
                });
            }
            return original.apply($location, [path]);
        }

        // compute window's height
        var computeHeight = function() {
            var maxHeight = Math.max($(window).height(), 500) - 180;
            $('.x-page-scope:eq(0)').css('height', maxHeight + 'px');
            $('.x-page-scope:eq(1)').css('height', maxHeight + 'px');
            $('.x-page-scope .x-editor pre').css('min-height', maxHeight + 'px');
            // http request size
            var subMaxHeight = maxHeight - 143;
            $('.x-page-scope .x-request').css('height', subMaxHeight + 'px');
            $('.x-page-scope .x-request .x-editor pre').css('min-height', subMaxHeight + 'px');
            // http response size
            $('.x-page-scope .x-response').css('height', (maxHeight - 57) + 'px');
        }
        $(window).bind('load resize', computeHeight);
        $rootScope.refresh = function() {
            $timeout(computeHeight);
        }

        // initialize menu
        $rootScope.init = function() {
            $('.navbar-header, .navbar-nav>li').each(function() {
                var $this = $(this);
                $this.removeClass('active');
                var href = $this.find('a').attr('href').substring(2);
                var path = $location.path() == '/' ? config.defaultPath : $location.path();
                if (path.indexOf(href) >= 0) {
                    $this.addClass('active');
                }
                $this.click(function() {
                    $('.navbar-header,.navbar-nav>li').removeClass('active');
                    $this.addClass('active');
                });
            });
        }
    });

    return angularAMD.bootstrap(app);
});
